package redis

import (
	"context"
)

// 删除一个或多个哈希表字段
func (h *Handler) HDel(key string, fields ...string) (count int64, err error) {
	count, err = h.Client.HDel(context.Background(), key, fields...).Result()
	go h.Log("Hash-HDel", []interface{}{key, fields}, count, err)
	return
}

// 查看哈希表 key 中，指定的字段是否存在
func (h *Handler) HExists(key string, field string) (has bool, err error) {
	has, err = h.Client.HExists(context.Background(), key, field).Result()
	go h.Log("Hash-HExists", []interface{}{key, field}, has, err)
	return
}

// 获取存储在哈希表中指定字段的值
func (h *Handler) HGet(key string, field string) (value string, err error) {
	value, err = h.Client.HGet(context.Background(), key, field).Result()
	go h.Log("Hash-HGet", []interface{}{key, field}, value, err)
	return
}

// 获取在哈希表中指定 key 的所有字段和值
func (h *Handler) HGetAll(key string) (fieldValue map[string]string, err error) {
	fieldValue, err = h.Client.HGetAll(context.Background(), key).Result()
	go h.Log("Hash-HGetAll", key, fieldValue, err)
	return
}

// 为哈希表 key 中的指定字段的整数值加上增量 increment，返回结果值
func (h *Handler) HIncrBy(key string, field string, incr int64) (value int64, err error) {
	value, err = h.Client.HIncrBy(context.Background(), key, field, incr).Result()
	go h.Log("Hash-HIncrBy", []interface{}{key, field, incr}, value, err)
	return
}

// 为哈希表 key 中的指定字段的浮点数值加上增量 increment
func (h *Handler) HIncrByFloat(key string, field string, incr float64) (value float64, err error) {
	value, err = h.Client.HIncrByFloat(context.Background(), key, field, incr).Result()
	go h.Log("Hash-HIncrByFloat", []interface{}{key, field, incr}, value, err)
	return
}

// 获取哈希表中所有的字段
func (h *Handler) HKeys(key string) (fields []string, err error) {
	fields, err = h.Client.HKeys(context.Background(), key).Result()
	go h.Log("Hash-HKeys", key, fields, err)
	return
}

// 获取哈希表中字段的数量
func (h *Handler) HLen(key string) (len int64, err error) {
	len, err = h.Client.HLen(context.Background(), key).Result()
	go h.Log("Hash-HLen", key, len, err)
	return
}

// 获取所有给定字段的值
func (h *Handler) HMGet(key string, fields ...string) (values []interface{}, err error) {
	values, err = h.Client.HMGet(context.Background(), key, fields...).Result()
	go h.Log("Hash-HMGet", []interface{}{key, fields}, values, err)
	return
}

// 同时将多个 field-value (域-值)对设置到哈希表 key 中
func (h *Handler) HMSet(key string, fieldValues ...interface{}) (status bool, err error) {
	status, err = h.Client.HMSet(context.Background(), key, fieldValues...).Result()
	go h.Log("Hash-HMSet", []interface{}{key, fieldValues}, status, err)
	return
}

// 将哈希表 key 中的字段 field 的值设为 value
func (h *Handler) HSet(key string, field string, value string) (len int64, err error) {
	len, err = h.Client.HSet(context.Background(), key, field, value).Result()
	go h.Log("Hash-HSet", []interface{}{key, field, value}, len, err)
	return
}

// 只有在字段 field 不存在时，设置哈希表字段的值
func (h *Handler) HSetNX(key string, field string, value string) (status bool, err error) {
	status, err = h.Client.HSetNX(context.Background(), key, field, value).Result()
	go h.Log("Hash-HSetNX", []interface{}{key, field, value}, status, err)
	return
}

// 获取哈希表中所有值
func (h *Handler) HVals(key string) (values []string, err error) {
	values, err = h.Client.HVals(context.Background(), key).Result()
	go h.Log("Hash-HVals", key, values, err)
	return
}

// 迭代获取哈希表中的键值对
func (h *Handler) HScan(key string, cursor uint64, match string, count int64) (fieldValues []string, nextCursor uint64, err error) {
	fieldValues, nextCursor, err = h.Client.HScan(context.Background(), key, cursor, match, count).Result()
	go h.Log("Hash-HScan", []interface{}{key, cursor, match, count}, []interface{}{fieldValues, nextCursor}, err)
	return
}
